%option noyywrap

%x COMMENT
%x HEX
%x BINARY

digit   [0-9]
xdigit  [0-9a-fA-F]
bin     [01]
octal   [0-7]

reg     ("%"[r][0-63])
vreg    ("%"[v][r][0-63])
svr     ("%"[s][v][r][0-15])

predicate ([@][!]?[p-s])
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ptx.tab.h"
%}

%%
"//".* {
    ; // 忽略注释
}
"/*"{
    BEGINE(COMMENT); //进入注释块状态   
}
<COMMENT> "*/"{
    BEGINE(INITIAL);
}
<COMMENT>([^*]|\n)+|.
{
    ;//忽略注释块内容
}
<COMMENT><<EOF>>
{
   printf("%s:%d: Unternimsted comment \n",
   currrentfilename, yylineno ) ;
    return 0;
}//发现和报告没有终结的注释

[\t ]+ {
    ; // 忽略空格和TAB
}
\n {
    yylineno++; // 统计行号
}
//处理分号、逗号、括号 赋值符号 
";" {
    return SEMICOLON;
}
"," {
    return COMMA;
}
"{" {
    return L_BRACE;
}
"}" {
    return R_BRACE;
}
"=" {
    return ASSGN;
}
"(" {
    return L_PAREN;
}
")" {
    return R_PAREN;
}
//以下为指示性语句的关键字
".alias" {
    return ALIAS;
}
".align" {
    return ALIGN;
}
".branchtargets" {
    return BRANCHTARGETS;
}
".callprototype" {
    return CALLPROTOTYPE;
}
".calltargets" {
    return CALLTARGETS;
}
".datalayout" {
    return DATALAYOUT;
}
".entry" {
    return ENTRY;
}
".extern" {
    return EXTERN;
}
".source_file" {
    return SRC_FILE;
}
".func" {
    return FUNC;
}
".global" {
    return GLOBAL;
}
".loc" {
    return LOC;
}
 ".local" {
    return LOCAL;
}
".maxnreg" {
    return MAXNREG;
}
".noreturn" {
    return NORETURN;
}
".param" {
    return PARAM;
}

".pragma" {
    return PRAGMA;
}
".reg" {
    return REG;
}
".section" {
    return SECTION;
}
".shared" {
    return SHARED;
}
".vreg" {
    return VREG;
}
".target" {
    return TARGET;
}
".version" {
    return VERSION;
}

".visible" {
    return VIZ;
}

".weak" {
    return WEAK;
}


//处理指令部分
/*谓词操作*/
{predicate}{
    return PREDICATEOP
}
/*一元操作指令*/
"log" {
    return LOGOP;
}
"sin" {
    return SINOP;
}
"exp" {
    return EXPOP;
}
"abs" {
    return ABSOP;
}
"cast" {
    return CASTOP;
}
"mov" {
    return MOVOP;
}
"brodcast" {
    return BROADCASTOP;
}
/*二元操作指令*/
"add" {
    return ADDOP;
}
"sub" {
    return SUBOP;
}
"mul" {
    return MULOP;
}
"cmpe" {
    return CMPEOP;
}
"cmpl" {
    return CMPLOP;
}
"cmpg" {
    return CMPGOP;
}
"div" {
    return DIVOP;
}
"and" {
    return ANDOP;
}
"or" {
    return OROP;
}
"not" {
    return NOTOP;
}
"xor" {
    return XOROP;
}
"shl" {
    return SHLOP;
}
"shr" {
    return SHROP;
}
"max" {
    return MAXOP;
}
"min" {
    return MINOP;
}
"rcp" {
    return RCPOP;
}
"rsp" {
    return RSPOP;
}
/*流控指令*/
"load" {
    return LOADOP;
}
"store" {
    return STOREOP;
}
/*堆栈操作*/
"alloc" {
    return ALLOCAOP;
}
/*特殊操作指令*/
"shuffle" {
    return SHUFFLEOP;
}
"reduce" {
    return REDUCEOP;
}
"dot" {
    return DOTOP;
}
"dma" {
    return DMAOP;
}

//标识符
[a-zA-Z_][a-zA-Z0-9_]*|[_%][a-zA-Z0-9_]+{
    yylval.ident = strdup(yytext);
    return IDENTIFIER;
}

//常量

//整形常量
{digit}+ {
    yylval.num = atoi(yytext);  //十进制常量
    return INTEGER;
}
"0"([xX]{xdigit}+[Uu]?){
  char* endptr；
  yylval.num = strtol(yytex, &endptr, 16);  //十六进制常量 
  return INTEGER;
}
"0"{bin}+[Uu]?{
  char* endptr；
  yylval.num = strtol(yytex, &endptr, 2);  //二进制常量 
  return INTEGER;
}
"0"{octal}+[Uu]?{
  char* endptr；
  yylval.num = strtol(yytex, &endptr, 8);  //八进制常量 
  return INTEGER;
}
//浮点常量（如何表示）？

//谓词常量
"true"|"fale"{
    return PREDICATE_CONST
}
//常量表达式 （所有符号都需要单独作为一个token？？？）
//OPERATOR  ([+*-/%=,;!<>(){}])



//数据空间
".reg"{
return REGTYPE;//与指示性语的 .reg 冲突
}
".vreg"{
return VREGTYPE;
}
".gsm"{
return GSMTYPE;
}
".am"{
return AMTYPE;
}
".sm"{
return SMTYPE;
}
".svr"{
return SVRTYPE;
}
".ddr"{
return DDRTYPE;
}

//数据类型

//标量数据类型
"i32" {
    return I32;
}
"i64" {
    return I64;
}
"u32" {
    return U32;
}
"u64" {
    return U64;
}
"f16" {
    return F16;
}
"f32" {
    return F32;
}
"f64" {
    return F64;
}
"bool" {
    return BOOL;
}

//向量数据类型


{reg} {
    return REG;
}
{vreg} {
    return VREG;
}
{svr} {
    return SVREG;
}
%%

int yywrap(void) {
    return 1;
}